using System;
using System.Web;
using LabNet.Server.Contract;
using LabNet.Server.Data;
using LabNet.Server.Domain;
using LabNet.Server.Infrastracture;
using LabNet.Web.Scffolding;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(NinjectWebCommon), "Stop")]

namespace LabNet.Web.Scffolding
{
    public static class NinjectWebCommon
    {
        private static readonly Bootstrapper Bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof (OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof (NinjectHttpModule));
            Bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            Bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {

            //kernel
            //    .Bind<IDataContext>()
            //    .To<DefaultContext>()
            //    .InRequestScope();


            kernel
                .Bind<IUnitOfWork>()
                .To<UnitOfWork>()
                .InRequestScope();


            kernel
                .Bind<IDatabaseFactory>()
                .To<DatabaseFactory>()
                .InRequestScope();




            kernel
                .Bind<IUserRepository>()
                .To<UserRepository>()
                .InRequestScope();


            kernel
                .Bind<IUserService>()
                .To<UserService>()
                .InRequestScope();

            kernel
                .Bind<IRoleService>()
                .To<RoleService>()
                .InRequestScope();

            kernel
                .Bind<IRoleRepository>()
                .To<RoleRepository>()
                .InRequestScope();

            kernel
                .Bind<IGroupService>()
                .To<GroupService>()
                .InRequestScope();

            kernel
                .Bind<IGroupRepository>()
                .To<GroupRepository>()
                .InRequestScope();
        }
    }
}
